package com.neu.questionnaireplus.config.session;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/** 
* @Description: 将session存放在sessionMap中 
* @Param:  
* @return:  
* @Author: ~FHX~ (:TSunS:)
* @Date: 2022/10/26 
*/ 
public class MySessionContext {
  
  private static Map<String, HttpSession> sessionMap = new HashMap<>();

  /** 
  * @Description: 向map中加入session 
  * @Param: [session] 
  * @return: void 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public static synchronized void addSession(HttpSession session) {
    if (session != null) {
      sessionMap.put(session.getId(), session);
    }
  }

  /** 
  * @Description: 从map中删除session 
  * @Param: [session] 
  * @return: void 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public static synchronized void delSession(HttpSession session) {
    if (session != null) {
      sessionMap.remove(session.getId());
    }
  }
  /** 
  * @Description: 获取session 
  * @Param: [sessionID] 
  * @return: javax.servlet.http.HttpSession 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  public static synchronized HttpSession getSession(String sessionID) {
    if (sessionID == null) {
      return null;
    }
    return sessionMap.get(sessionID);
  }


}

